$(function () {
    init();
    function init() {
        $(".lt_register").on("tap", function (e) {
            var userName = $.trim($(".userName").val());
            var password = $.trim($(".password").val());
            var con_password = $.trim($(".con_password").val());
            var codeTxt = $.trim($(".codeTxt").val());
            var chkBox = $(".isAgreeChk:checked");
            if (!checkPhone(userName)) {
                mui.toast("手机号码不合法");
                return;
            }

            if (password.length < 6) {
                mui.toast("密码不合法");
                return;
            }

            if (password != con_password) {
                mui.toast("密码不一致");
                return;
            }

            if (codeTxt.length != 6) {
                mui.toast("认证码不合法");
                return;
            }

            // 判断是否同意
            if (chkBox.length < 1) {
                mui.toast("哥们,不同意不行");
                return;
            }

            var queryObj = {
                username: userName,
                password: password,
                mobile: userName,
                vCode: codeTxt
            }

            $.post("/user/register", queryObj, function (res) {
                if (res.success) {
                    // 成功
                    location.href = "../index.html";
                } else {
                    mui.toast(result.message);
                    return;
                }
            })
        });
        $(".getCode").on("tap", function (e) {
            if ($(this).hasClass("mui-disabled")) {
                return;
            }
            $(this).addClass("mui-disabled");
            $(this).html("正在发送...");
            var that = this;
            $.get("/user/vCode", function (res) {
                var time = 4;
                var timeId = setInterval(function (e) {
                    time--;
                    $(that).html(time + "秒后再获取");
                    if(time<0){
                        $(that).removeClass("mui-disabled");
                        $(that).html("获取认证码")
                    }
                }, 1000)
            })
        })
    }
    function checkPhone(phone) {
        var phoneReg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;
        //电话  
        if (!phoneReg.test(phone)) {
            // 不满足
            return false;
        } else {
            return true;
        }
    }
})